﻿using BrnMall.Manager.Domain.AddressBook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static BrnMall.Manager.Domain.AddressBook.ClientTag;

namespace BrnMall.Manager.Manager.AddressBook
{
    /// <summary>
    /// 客户标签管理
    /// </summary>
    public class ClientTagManager
    {
        static string TableName = "crm_ClientTag";
        public static int Add(ClientTag model)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(model);
            }
            return model.Id;
        }

        public static int Delete(int Id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Execute(string.Format("delete from {0} where Id={1}", TableName, Id));
            }
        }

        public static int Update(ClientTag model) {
            var sql = new StringBuilder();
            sql.AppendFormat("update {0} set ",TableName);
            if (!string.IsNullOrEmpty(model.TagName))
                sql.AppendFormat("tagname='{0}',",model.TagName);
            if (model.CategoryId != 0)
                sql.AppendFormat("categoryId={0},",model.CategoryId);
            if (model.StaffId != 0)
                sql.AppendFormat("staffId={0},",model.StaffId);
            sql.AppendFormat("addtime=getdate() where Id={0}",model.Id);
            using (var db = new QDMallDatabase())
            {
                return db.Execute(sql.ToString());
            }
        }


        public static bool Exists(int staffId, string tagName)
        {
            object obj = null;
            using (var db = new QDMallDatabase())
            {
                obj = db.ExecuteScalar<object>($"select top 1 id from {TableName} where (staffId=@0 and TagName=@1) or (TagName=@1 and staffId=0)", staffId, tagName);
            }
            if (obj != null && obj != DBNull.Value)
                return true;
            return false;
        }


        /// <summary>
        /// 追加点赞数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="count"></param>
        public static void AppendLikeCount(int id, int count)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute(string.Format("update {0} set likeCount=likeCount+{1} where id={2}", TableName, count, id));
            }
        }

        public static List<ClientTag> GetList(int staffId, int page, int pageSize = 10)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<ClientTag>(page, pageSize, string.Format("select * from {0} where staffId=0 or staffId={1}", TableName, staffId));
            }
        }

        /// <summary>
        /// 根据ID获取对应的信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static List<ClientTagView> GetClientTag(int staffId, int clientId)
        {
            using (var db = new QDMallDatabase())
            {
               string sql=string.Format("select temp1.TagName,temp1.CategoryId,temp1.addtime,temp2.staffId,temp2.Clientid from  crm_ClientTag temp1 left join crm_ClientRelations temp2 on temp1.id=temp2.ClientTagid where temp2.Clientid={0} and temp2.staffId={1}", clientId, staffId);
                return db.Fetch<ClientTagView>(sql);
            }
        }
        public static ClientTag GetClientTagById(int Id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<ClientTag>(string.Format("select * from {0} where Id={1}", TableName, Id));
            }
        }

    }
}